#!/usr/bin/env python3
#
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
#

from qiling.const import QL_ARCH
from qiling.os.posix.posix import SYSCALL_PREF

def map_syscall(ql, syscall_num):
    if ql.archtype == QL_ARCH.ARM:
        for k, v in syscall_table.items():
            if v == syscall_num:
                return f'{SYSCALL_PREF}{k}'

# Source: https://github.com/vocho/openqnx
# trunk/services/system/public/sys/kercalls.h
syscall_table = {
    "clock_cycles": (0xfe000000),
    "trace_event": (0x01),
    "ring0": (0x02),
    "spare1": (0x03),
    "spare2": (0x04),
    "spare3": (0x05),
    "spare4": (0x06),
    "sys_cpupage_get": (0x07),
    "sys_cpupage_set": (0x08),
    "sys_spare1": (0x09),
    "msg_current": (0x0a),
    "msg_sendv": (0x0b),
    "msg_sendvnc": (0x0c),
    "msg_error": (0x0d),
    "msg_receivev": (0x0e),
    "msg_replyv": (0x0f),
    "msg_readv": (0x10),
    "msg_writev": (0x11),
    "msg_readwritev": (0x12),
    "msg_info": (0x13),
    "msg_send_pulse": (0x14),
    "msg_deliver_event": (0x15),
    "msg_keydata": (0x16),
    "msg_readiov": (0x17),
    "msg_receivepulsev": (0x18),
    "msg_verify_event": (0x19),
    "signal_kill": (0x1a),
    "signal_return": (0x1b),
    "signal_fault": (0x1c),
    "signal_action": (0x1d),
    "signal_procmask": (0x1e),
    "signal_suspend": (0x1f),
    "signal_waitinfo": (0x20),
    "signal_spare1": (0x21),
    "signal_spare2": (0x22),
    "channel_create": (0x23),
    "channel_destroy": (0x24),
    "chancon_attr": (0x25),
    "channel_spare1": (0x26),
    "connect_attach": (0x27),
    "connect_detach": (0x28),
    "connect_server_info": (0x29),
    "connect_client_info": (0x2a),
    "connect_flags": (0x2b),
    "connect_spare1": (0x2c),
    "connect_spare2": (0x2d),
    "thread_create": (0x2e),
    "thread_destroy": (0x2f),
    "thread_destroyall": (0x30),
    "thread_detach": (0x31),
    "thread_join": (0x32),
    "thread_cancel": (0x33),
    "thread_ctl": (0x34),
    "thread_spare1": (0x35),
    "thread_spare2": (0x36),
    "interrupt_attach": (0x37),
    "interrupt_detach_func": (0x38),
    "interrupt_detach": (0x39),
    "interrupt_wait": (0x3a),
    "interrupt_mask": (0x3b),
    "interrupt_unmask": (0x3c),
    "interrupt_spare1": (0x3d),
    "interrupt_spare2": (0x3e),
    "interrupt_spare3": (0x3f),
    "interrupt_spare4": (0x40),
    "clock_time": (0x41),
    "clock_adjust": (0x42),
    "clock_period": (0x43),
    "clock_id": (0x44),
    "clock_spare2": (0x45),
    "timer_create": (0x46),
    "timer_destroy": (0x47),
    "timer_settime": (0x48),
    "timer_info": (0x49),
    "timer_alarm": (0x4a),
    "timer_timeout": (0x4b),
    "timer_spare1": (0x4c),
    "timer_spare2": (0x4d),
    "sync_create": (0x4e),
    "sync_destroy": (0x4f),
    "sync_mutex_lock": (0x50),
    "sync_mutex_unlock": (0x51),
    "sync_condvar_wait": (0x52),
    "sync_condvar_signal": (0x53),
    "sync_sem_post": (0x54),
    "sync_sem_wait": (0x55),
    "sync_ctl": (0x56),
    "sync_mutex_revive": (0x57),
    "sched_get": (0x58),
    "sched_set": (0x59),
    "sched_yield": (0x5a),
    "sched_info": (0x5b),
    "sched_ctl": (0x5c),
    "net_cred": (0x5d),
    "net_vtid": (0x5e),
    "net_unblock": (0x5f),
    "net_infoscoid": (0x60),
    "net_signal_kill": (0x61),
    "net_spare1": (0x62),
    "net_spare2": (0x63),
    "mt_ctl": (0x64),
    "bad": (0x65),
}
